from django.db import models
from django.contrib.auth.models import User


class Schedule(models.Model):
    """日程安排"""
    SCHEDULE_TYPE_CHOICES = [
        ('course', '课程'),
        ('meeting', '会议'),
        ('task', '任务'),
        ('other', '其他'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='schedules', verbose_name='用户', db_comment='用户ID')
    title = models.CharField(max_length=200, verbose_name='标题', db_comment='标题')
    description = models.TextField(null=True, blank=True, verbose_name='描述', db_comment='描述')
    schedule_type = models.CharField(max_length=20, choices=SCHEDULE_TYPE_CHOICES, verbose_name='类型', db_comment='类型:course课程,meeting会议,task任务,other其他')
    start_time = models.DateTimeField(verbose_name='开始时间', db_comment='开始时间')
    end_time = models.DateTimeField(verbose_name='结束时间', db_comment='结束时间')
    location = models.CharField(max_length=200, null=True, blank=True, verbose_name='地点', db_comment='地点')
    is_recurring = models.BooleanField(default=False, verbose_name='是否重复', db_comment='是否重复')
    recurrence_rule = models.CharField(max_length=100, null=True, blank=True, verbose_name='重复规则', db_comment='重复规则')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '日程安排'
        verbose_name_plural = '日程安排'
        db_table = 'zhahuo_schedule'
        db_table_comment = '日程安排表'
        ordering = ['start_time']

    def __str__(self):
        return f'{self.user.username} - {self.title}'


class CourseTable(models.Model):
    """课程表"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='courses', verbose_name='用户', db_comment='用户ID')
    course_name = models.CharField(max_length=100, verbose_name='课程名', db_comment='课程名')
    teacher = models.CharField(max_length=50, null=True, blank=True, verbose_name='任课教师', db_comment='任课教师')
    location = models.CharField(max_length=100, null=True, blank=True, verbose_name='上课地点', db_comment='上课地点')
    day_of_week = models.IntegerField(verbose_name='星期几', db_comment='星期几(1-7)')
    start_time = models.TimeField(verbose_name='开始时间', db_comment='开始时间')
    end_time = models.TimeField(verbose_name='结束时间', db_comment='结束时间')
    semester = models.CharField(max_length=20, verbose_name='学期', db_comment='学期')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        verbose_name = '课程表'
        verbose_name_plural = '课程表'
        db_table = 'zhahuo_course_table'
        db_table_comment = '课程表'

    def __str__(self):
        return f'{self.course_name} - {self.get_day_display()}'
